package Math;

public class _367_ValidPerfectSquare {
    //bad solution:use math's sqrt method
    public boolean isPerfectSquare_0(int num) {
        double sqrt = Math.sqrt(num);
        return sqrt == (double)(num / (int)sqrt);
    }

    //solution 2:use binary seawrch
    public boolean isPerfectSquare(int num) {
        if (num > 2147395600) {
            return false;
        }
        int low = 1, high = 46340;
        while (low <= high) {
            int middle = low + (high - low) / 2;
            int power = middle*middle;
            if (power == num) {
                return true;
            } else if (power < num) {
                low = middle + 1;
            } else {
                high = middle - 1;
            }
        }
        return false;
    }
}
